#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2007 Google Inc.
#
#
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail
import cgi
import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.api import urlfetch
import urllib # Used to unescape URL parameters.
#import settings

try:
  from xml.etree import ElementTree
except ImportError:
  from elementtree import ElementTree
import gdata.calendar.service#Google data calender
import gdata.service
import atom.service
import gdata.calendar
import atom
import getopt
import sys
import string
import time
import datetime
from google.appengine.api import urlfetch
#import ssl


class Xbook(webapp.RequestHandler):
    def getonepage(self,turl):
#        print(turl)
        req=urlfetch.fetch(turl,method=urlfetch.GET,headers={},allow_truncated=True,follow_redirects=True)
        content=req.content
        
        s=content.find('id="discount"')
        e=content.find('<!--discount end-->',s)
        ulist=[]
        ulist=content[s:e].split('class="list-h"')
#        print(len(ulist))
#        print(ulist[1])
        return ulist[1]
#        print(content[s:e])
        
    def get(self):
        self.curtime=str(datetime.datetime.now())
#        print('start at:',datetime.datetime.now())
    #    sendsms2me('start at:'+str(datetime.now()))
    #    while 1:http://www.360buy.com/products/1713-3258-000.html
        startpage=3258
        endpage=3296
        print(startpage,endpage)
        r=[]
        urllist=[]
        for e in range(startpage,endpage):
            urllist.append('http://www.360buy.com/products/1713-'+str(e)+'-000.html')
        rlist=[]
        for e in urllist:
            rlist.append(self.getonepage(e))
        
        for e in rlist:
            print(e)
#            print(e)
#            break

#    def hktouch(self):    
#        hkurl='http://store.apple.com/hk-zh/browse/home/shop_iphone/family/iphone'
#    #    cnurl='http://store.apple.com/cn/browse/home/shop_iphone/family/iphone'
#        req=urlfetch.fetch(hkurl,method=urlfetch.GET,headers={},allow_truncated=False,follow_redirects=False)
#        content=req.content.decode("utf8")
#    #    print(content)
#        i=content.find('4,988')
#        p=content.find('customer_commit_display',i)
#        e=content.find('</span>',p)
#        ns=content[p+len('customer_commit_display')+2:e]
#        tag='暫無供應'.decode('utf8')
##        self.response.out.write('<'+ns+'>'+tag)        
#        if ns==tag:
#            self.response.out.write(self.curtime+'--hk not exist iphone4<br>')
#        else:
#            self.response.out.write(self.curtime+'--hk has apple now<br>')
#            sendsms2me('hk has apple now')
#    def cntouch(self):
#        cnurl='http://store.apple.com/cn/browse/home/shop_iphone/family/iphone'
#        req=urlfetch.fetch(cnurl,method=urlfetch.GET,headers={},allow_truncated=False,follow_redirects=False)
#        content=req.content.decode("utf8")
##        print(content)
#        i=content.find('4,999')
#        p=content.find('customer_commit_display',i)
#        e=content.find('</span>',p)
#        ns=content[p+len('customer_commit_display')+2:e] 
#        tag='暂无供应'.decode('utf8')
##        self.response.out.write('<'+ns+'>'+tag)        
#        if ns==tag:
#            self.response.out.write(self.curtime+'--cn not exist iphone4<br>')
#        else:
#            self.response.out.write(self.curtime+'--cn has apple now<br>')
#            sendsms2me('cn has apple now')

def main():
    application = webapp.WSGIApplication([('/book',Xbook)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()